package com.project.rfidpage.controller;


import com.project.rfidpage.aop.TagLog;
import com.project.rfidpage.dto.BgGjxlParam;
import com.project.rfidpage.entity.BgGjxl;
import com.project.rfidpage.service.BgGjxlService;
import com.project.rfidpage.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/rfidpage/bggjxl/")
public class BgGjxlController extends BaseController {

    @Resource
    BgGjxlService bgGjxlService;

    /**
     * 列表查询
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询基站关联公交站点列表")
    public RestResult page(@RequestBody BgGjxlParam param) {
        this.getPage(false, param);
        return RestResult.ok(bgGjxlService.getListForPage(param));
    }

    /**
     * 新增基站关联公交站点
     */
    @PutMapping("add")
    @TagLog(operation = 2, content = "新增基站关联公交站点")
    public RestResult add(@RequestHeader(value = "user") String user, @RequestBody BgGjxl param) {
        List<BgGjxl> list = bgGjxlService.getByRepeat(param.getBgno(), param.getJgxlid(), param.getZdlx(), param.getXh());
        if (list.size() == 0) {
            Integer jgzdid = bgGjxlService.getJgzdid(param);
            if (jgzdid != null) {
                try {
                    param.setJgzdid(jgzdid);
                    param.setUser_id(user);
                    param.setUpdt(new Date());
                    bgGjxlService.insert(param);
                    return RestResult.ok();
                } catch (Exception e) {
                    return RestResult.error(e.getMessage());
                }
            } else {
                return RestResult.error("公交站点不存在");
            }
        } else {
            return RestResult.error("基站关联公交站点已经存在");
        }
    }

    /**
     * 删除基站关联公交站点
     */
    @PutMapping("delete")
    @TagLog(operation = 4, content = "删除基站关联公交站点")
    public RestResult delete(@RequestHeader(value = "user") String user, @RequestBody BgGjxl param) {
        List<BgGjxl> list = bgGjxlService.getByRepeat(param.getBgno(), param.getJgxlid(), param.getZdlx(), param.getXh());
        if (list.size() == 0) {
            return RestResult.error("该条数据不存在");
        } else {
            try {
                bgGjxlService.delete(param);
                return RestResult.ok();
            } catch (Exception e) {
                return RestResult.error(e.getMessage());
            }
        }
    }
}
